/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.woodws.fresh.im.controller

import com.woodws.fresh.common.core.domain.QueryBody
import com.woodws.fresh.common.core.domain.QueryParams
import com.woodws.fresh.common.core.domain.R
import com.woodws.fresh.common.security.util.SecurityContextUtils
import com.woodws.fresh.common.web.controller.BaseController
import com.woodws.fresh.im.api.dto.ImMessageDto
import com.woodws.fresh.im.api.service.ImMessageService
import org.springframework.web.bind.annotation.*
import io.swagger.annotations.Api

/**
 * 消息表控制层
 * @author maoxiaodong
 */
@Api(value = "imMessage", tags = ["消息表"])
@RestController
@RequestMapping("/imMessage")
class ImMessageController : BaseController<ImMessageDto, ImMessageDto, ImMessageService>(){

    /**
     * 查询登录用户最近聊天记录
     */
    @GetMapping("/findByLast")
    fun findMyByLast(): R<List<ImMessageDto>>{
        val  user = SecurityContextUtils.user()
        return service.findByFromIdGroupByToId(user.id!!)
    }

    /**
     * 查询聊天信息
     * @param toId 好友ID
     */
    @GetMapping("/findByFromIdAndToId")
    fun findByFromIdAndToId(toId: Long): R<List<ImMessageDto>>{
        val  user = SecurityContextUtils.user()
        return service.findByFromIdAndToId(user.id!!, toId)
    }

    /**
     * 查询登录用户和好友最近聊天记录
     */
    @GetMapping("/findMyByQueryParams")
    fun findMyByQueryParams(params: QueryParams<Any>): R<QueryBody<ImMessageDto>>{
        val  user = SecurityContextUtils.user()
        val map: MutableMap<String, Any> = params.params as MutableMap<String, Any>
        map["fromId"] = user.id!!
        return service.findByQueryParams(params)
    }
}